import { Create, Update, GetPages, GetDetail, Delete } from '../../services/authorities/user';
import { GetAll } from '../../services/authorities/department';
import { GetCheckModel } from '../../services/authorities/rolegroup';
import { message } from 'antd';
import { routerRedux } from 'dva/router';


export default {

    namespace: 'user',

    state: {
        postloading: false,
        pages: {},
        detail: {},
        schools: [],
        checkmodel:[]
    },

    subscriptions: {
        setup({ history, dispatch }) {
            // 监听 history 变化，当进入 `/` 时触发 `load` action
            return history.listen(({ pathname }) => {
                if (pathname == "/user") {
                    dispatch({ type: 'getpages', payload: { PageIndex: 1, PageSize: 10 } });
                }
                if (pathname.indexOf('/useredit') > -1) {
                    var arr = pathname.split('/');
                    var param = arr[arr.length - 1];
                    dispatch({ type: 'initselect' });
                    dispatch({ type: 'getcheckmodel' });
                    if (param == 'useredit') {
                        dispatch({ type: 'cleardeatil' });
                    }
                    else {
                      dispatch({ type: 'user/getdetail', payload: { guid: param } });
                    }

                }
            });
        },
    },

    effects: {
        *go({ payload }, { call, put }) {
            yield put(routerRedux.push(payload.url));
        },
        *save({ payload }, { call, put }) {
            yield put({ type: 'showloading' });
            const result = yield call(payload.guid == '' ? Create : Update, payload);
            yield put({ type: 'hideloading' });
            const { data } = result;
            if (data.success) {
                message.success(data.message);
                yield put(routerRedux.push('/user'));
            }
            else {
                message.error(data.message);
            }

        },
        *getpages({ payload }, { call, put }) {
            yield put({ type: 'showloading' });
            const result = yield call(GetPages, payload);
            yield put({ type: 'hideloading' });
            const { data } = result;
            if (data.success) {
                yield put({ type: 'setpages', payload: data.data });
            }
            else {
                message.error(data.message);
            }
        },
        *getdetail({ payload }, { call, put }) {
            yield put({ type: 'showloading' });
            const result = yield call(GetDetail, payload);
            yield put({ type: 'hideloading' });
            const { data } = result;
            if (data.success) {
                yield put({ type: 'setdetatil', payload: data.data });
            }
            else {
                message.error(data.message);
            }
        },
        *delete({ payload }, { call, put }) {
            yield put({ type: 'showloading' });
            const result = yield call(Delete, payload);
            yield put({ type: 'hideloading' });
            const { data } = result;
            if (data.success) {
                message.success(data.message);
                yield put(routerRedux.replace('/user'));
            }
            else {
                message.error(data.message);
            }
        },
        *cleardeatil({ payload }, { call, put }) {
            yield put({ type: 'clear' });
        },
        *initselect({ payload }, { call, put }) {
            const result = yield call(GetAll, payload);
            const { data } = result;
            if (data.success) {
                yield put({ type: 'setselect', payload: data.data });
            }
            else {
                message.error(data.message);
            }
        },
        *getcheckmodel({payload},{call,put})
        {
            const result = yield call(GetCheckModel, payload);
            const { data } = result;
            if (data.success) {
                yield put({ type: 'setcheck', payload: data.data });
            }
            else {
                message.error(data.message);
            }
        }

    },

    reducers: {
        showloading(state) {
            return { ...state, postloading: true };
        },
        hideloading(state) {
            return { ...state, postloading: false };
        },
        clear(state) {
            return { ...state, detail: {} };
        },
        setpages(state, { payload }) {
            return { ...state, pages: payload }
        },
        setdetatil(state, { payload }) {
            return { ...state, detail: payload }
        },
        setselect(state, { payload }) {
            return { ...state, schools: payload }
        },
        setcheck(state, { payload }) {
            return { ...state, checkmodel: payload }
        }

    },

};

